<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport( 'joomla.application.component.model' );
JTable::addIncludePath(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_auction'.DS.'tables');	

class AuctionModelList extends JModel
{
	var $_auctions = null;
	var $_auction = null;
	
	function __construct()
	{
		parent::__construct();
		$cid = JRequest::getVar('cid', 0);
		$this->_cid = $cid;	
	}
	
	function checkAuction(){
		$user = JFactory::getUser();
		$this->_db->setQuery('SELECT checkout_status, gid FROM #__users WHERE id = '.$user->id);
		$user_info = $this->_db->loadObject();

		/*if($user->get('guest') == 1){
			echo '<script>
					alert("Bedes du logge ind");
					location.href="index.php?option=com_user&view=login&Itemid=13";
				
			</script>';
		}*/
		/*if($user_info->gid == 18 && $user_info->checkout_status == 0){
			echo '<script>
				var go = confirm("Du har skal betale depostium for at byde. Vil du gå til betaling TRYK OK");
		
				if (go == true){
					location.href=\'index.php?option=com_user&view=register&layout=payment&userid='.$user->id.'&Itemid=13&active=1\';
				}
				else{
					location.href="index.php";
				}
			</script>';
		}*/

		return $user_info->gid;
	}
		
	function getAuction($auction_id=0){
		if($auction_id == 0){
			$query = "SELECT a.id FROM #__auction a INNER JOIN #__pr_product p ON a.product_id = p.id WHERE a.currentprice != 0 AND a.closed = 0 ORDER BY p.katno ASC LIMIT 0,1";
			$this->_db->setQuery($query);
			if($id = $this->_db->loadResult()){
				$query = "SELECT *, a.id as id FROM #__auction as a LEFT JOIN #__pr_product as p ON (a.product_id = p.id) WHERE a.id = ".$id;
			} else { 
				$query = "SELECT *, a.id as id FROM #__auction as a LEFT JOIN #__pr_product as p ON (a.product_id = p.id) WHERE a.published = 1 AND a.closed = 0 AND auction_date >= '".date("Y-m-d", time())."' ORDER BY a.id LIMIT 0,1";
			}
		} else {
			$query = "SELECT *, a.id as id FROM #__auction as a LEFT JOIN #__pr_product as p ON (a.product_id = p.id) WHERE a.id = ".$auction_id;
		}
		//die($query);
		$this->_db->setQuery($query);
		$this->_auction = $this->_db->loadObject();
		return $this->_auction;
	}
}
?>
